package master;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;


public class MasterLoginAction extends ActionSupport{
	public static Reader reader;
	public static SqlMapClient sqlMapper;
	
	private MasterMemberVO paramClass;
	private MasterMemberVO resultClass;
	
	private String login_id;
	private String password;
	private String project_manager_code;
	
	private int loginCheck;
	
	public MasterLoginAction() throws IOException{
		reader = Resources.getResourceAsReader("sqlMapConfig.xml");
		sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
		reader.close();
	}
	
	public String execute() throws Exception{
		paramClass = new MasterMemberVO();
		resultClass = new MasterMemberVO();
		
		resultClass = (MasterMemberVO)sqlMapper.queryForObject("getMasterMember", getLogin_id());
		
		if(resultClass == null){
			loginCheck = 0;
			return INPUT;
		}
		else{
			if(resultClass.getProject_manager_code().equals(project_manager_code)){
				if(resultClass.getPassword().equals(password)){
					loginCheck = 1;//3가지 조건 모두 충족
					ActionContext context = ActionContext.getContext();
					Map<String, Object> session = context.getSession();
					session.put("login_id", login_id);
					System.out.println(session);
					return SUCCESS;
				}
				else{
					loginCheck = -1;
					
					return INPUT;
				}
				
			}
			return INPUT;
		}		
	}

	public static Reader getReader() {
		return reader;
	}

	public static void setReader(Reader reader) {
		MasterLoginAction.reader = reader;
	}

	public static SqlMapClient getSqlMapper() {
		return sqlMapper;
	}

	public static void setSqlMapper(SqlMapClient sqlMapper) {
		MasterLoginAction.sqlMapper = sqlMapper;
	}

	public MasterMemberVO getParamClass() {
		return paramClass;
	}

	public void setParamClass(MasterMemberVO paramClass) {
		this.paramClass = paramClass;
	}

	public MasterMemberVO getResultClass() {
		return resultClass;
	}

	public void setResultClass(MasterMemberVO resultClass) {
		this.resultClass = resultClass;
	}

	public String getLogin_id() {
		return login_id;
	}

	public void setLogin_id(String login_id) {
		this.login_id = login_id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getProject_manager_code() {
		return project_manager_code;
	}

	public void setProject_manager_code(String project_manager_code) {
		this.project_manager_code = project_manager_code;
	}

	public int getLoginCheck() {
		return loginCheck;
	}

	public void setLoginCheck(int loginCheck) {
		this.loginCheck = loginCheck;
	}

	
}
